package com.wsz.user.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wsz.pojo.*;
import com.wsz.pojo.dto.PasswordDto;
import com.wsz.pojo.dto.RegisterUserDto;
import com.wsz.user.service.*;
import com.wsz.utils.DateUtil;
import com.wsz.utils.JwtUtils;
import com.wsz.utils.SecurityContextUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@AllArgsConstructor
@RestController
public class UserController {

    private final UserService userService;

    private final UserInfoService userInfoService;

    private final RecordService recordService;

    private final EssaysService essaysService;

    @PostMapping("/register")
    public R register(@RequestBody RegisterUserDto registerUserDto){
        return userService.register(registerUserDto);
    }

    @PostMapping("/token/refresh")
    public R refresh(@RequestParam("refresh") String refresh){
        DecodedJWT jwt = JwtUtils.parseToken(refresh);
        String username = jwt.getIssuer();
        User user = userService.getUserByUsername(username);
        String access = JwtUtils.createAccessToken(username, user.getId());
        return R.ok().data("access_token",access);
    }

    @GetMapping("/get/info/{id}")
    public R getUserInfo(@PathVariable("id") String id){
        UserInfo userinfo = userInfoService.getById(id);
        return R.ok().data("userinfo",userinfo);
    }

    @PostMapping("/update/info")
    public R updateInfo(@RequestBody UserFromDo userFromDo){
        return userInfoService.updateUserFromDo(userFromDo);
    }

    @GetMapping("/getuserfrom")
    public R getUserFrom(){
        String userId = SecurityContextUtils.getUserId();
        UserInfo userinfo = userInfoService.getById(userId);

        UserFromDo userFromDo = new UserFromDo();
        BeanUtils.copyProperties(userinfo,userFromDo);

        return R.ok().data("userfrom",userFromDo);
    }

    @PutMapping("/avatar")
    public R updateAvatar(@RequestParam("avatar") String avatar){
        return userInfoService.updateAvatar(avatar);
    }

    @PutMapping("/password")
    public R updatePassword(@RequestBody PasswordDto passwordDto){
        return userService.updatePassword(passwordDto);
    }

    //获取后台用户列表
    @GetMapping("/get/users/{page}/{limit}")
    public R userList(@PathVariable Long page,
                      @PathVariable Long limit){
        return userService.getUserList(page,limit);
    }

    //封禁用户
    @PostMapping("/update/enabled")
    public R updateEnabled(@RequestParam String userId, @RequestParam Boolean enabled){
        if(enabled == null || userId == null) return R.error().message("参数错误");
        userService.updateEabled(userId,enabled);
        return R.ok();
    }

    //统计
    @GetMapping("/ucenter/count")
    public R getUcenterCount(@RequestParam String date){
        Integer registerCount = userService.getRegisterCount(date);
        Integer pkCount= recordService.getPkCount(date);
        Integer essaysCount = essaysService.getEssaysCount(date);

        Ucenter ucenter = new Ucenter();
        ucenter.setDate(date);
        ucenter.setRegisterCount(registerCount);
        ucenter.setEssaysCount(essaysCount);
        ucenter.setRecordCount(pkCount);
        return R.ok().data("ucenter",ucenter);
    }

}
